{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install google-generativeai qdrant-client -qq"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import google.generativeai as gemini_client\n",
    "from qdrant_client import QdrantClient\n",
    "from qdrant_client.http.models import Distance, PointStruct, VectorParams"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "collection_name = \"example_collection\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "GEMINI_API_KEY = \"your_api_key\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "gemini_client.configure(api_key=GEMINI_API_KEY)\n",
    "search_client = QdrantClient(\":memory:\")\n",
    "texts = [\n",
    "    \"Qdrant is a vector database that is compatible with Gemini.\",\n",
    "    \"Gemini is a new family of Google PaLM models, released in December 2023.\",\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "results = [\n",
    "    gemini_client.embed_content(\n",
    "        model=\"models/embedding-001\",\n",
    "        content=sentence,\n",
    "        task_type=\"retrieval_document\",\n",
    "        title=\"Qdrant x Gemini\",\n",
    "    )\n",
    "    for sentence in texts\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "768"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(results[0]['embedding'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "points = [\n",
    "    PointStruct(\n",
    "        id=idx,\n",
    "        vector=response['embedding'],\n",
    "        payload={\"text\": text},\n",
    "    )\n",
    "    for idx, (response, text) in enumerate(zip(results, texts))\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "UpdateResult(operation_id=0, status=<UpdateStatus.COMPLETED: 'completed'>)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search_client.create_collection(collection_name, vectors_config=\n",
    "    VectorParams(\n",
    "        size=768,\n",
    "        distance=Distance.COSINE,\n",
    "    )\n",
    ")\n",
    "search_client.upsert(collection_name, points)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[ScoredPoint(id=0, version=0, score=0.7274398065331633, payload={'text': 'Qdrant is the best vector search engine!'}, vector=None, shard_key=None),\n",
       " ScoredPoint(id=1, version=0, score=0.7153888047955659, payload={'text': 'Loved by Enterprises and everyone building for low latency, high performance, and scale.'}, vector=None, shard_key=None)]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search_client.search(\n",
    "    collection_name=collection_name,\n",
    "    query_vector=gemini_client.embed_content(\n",
    "        model=\"models/embedding-001\",\n",
    "        content=\"Is Qdrant compatible with Gemini?\",\n",
    "        task_type=\"retrieval_query\",\n",
    "    )[\"embedding\"],\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "examples",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
